package com.ruan.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.common.Code;
import com.ruan.common.Result;
import com.ruan.mapper.AdminMapper;
import com.ruan.mapper.EmployeeMapper;
import com.ruan.mapper.ToolBorrowRecordsMapper;
import com.ruan.mapper.ToolsMapper;
import com.ruan.pojo.*;
import com.ruan.service.ToolBorrowRecordsService;
import com.ruan.service.ToolsService;
import com.ruan.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import javax.annotation.Resource;


@Service
@Transactional
public class ToolBorrowRecordsServiceImpl extends ServiceImpl<ToolBorrowRecordsMapper, ToolBorrowRecords> implements ToolBorrowRecordsService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private ToolsMapper toolsMapper;

    @Autowired
    private ToolBorrowRecordsMapper toolBorrowRecordsMapper;

    @Resource
    private AdminMapper adminMapper;


    /**
     * 提交工具借用申请
     * @param toolBorrowRecords
     */
    @Override
    public Result appForToolBR(ToolBorrowRecords toolBorrowRecords) {
//        System.out.println("appForToolBR提交过来的参数是："+toolBorrowRecords);
        toolBorrowRecords.setSubmitTime(new Date());
        Integer bnum = toolBorrowRecords.getBnum();//要借的工具数量
        //获取这种工具的库存
        Long toolId = toolBorrowRecords.getToolId();
        Tools tools = toolsMapper.selectById(toolId);
      if(tools != null){
            Integer total = tools.getTotal();
        if (bnum <= total){
            toolBorrowRecords.setStatus("待审核");
            toolBorrowRecords.setToolStatus(3);//未出库
            toolBorrowRecordsMapper.insert(toolBorrowRecords);
            return new Result(Code.SAVE_OK,"提交成功，待管理员审核");
        }else {
            return new Result(Code.SAVE_ERR,"库存不足！");
        }
      }
        return new Result(Code.SAVE_ERR,"找不到工具！");
    }


    //工具归还申请
    @Override
    public String backAudit(Long id) {//,Integer adminId
        ToolBorrowRecords toolBorrowRecords = toolBorrowRecordsMapper.selectById(id);
        //审核之后才填入
//        Admin admin = adminMapper.selectById(adminId);
//        toolBorrowRecords.setApprover(admin.getUsername());
//        Long toolId = toolBorrowRecords.getToolId();
//        Tools tool = toolsMapper.selectById(toolId);
        toolBorrowRecords.setToolStatus(4);//0:已出库，1:使用中，2:已归还，3:待归还，4:归还中
        toolBorrowRecords.setStatus("归还中");
//        tool.setTotal(tool.getTotal() + toolBorrowRecords.getBnum());
//        tool.setLent(tool.getLent() - toolBorrowRecords.getBnum());
        toolBorrowRecordsMapper.updateById(toolBorrowRecords);
//        toolsMapper.updateById(tool);//更新工具库存
        return "工具归还申请成功";
    }

    /**
     * 工具归还
     * @param id 借用记录id
     * @param adminId 管理员id
     */
    @Override
    public String back(Long id,Integer adminId) {
        ToolBorrowRecords toolBorrowRecords = toolBorrowRecordsMapper.selectById(id);

        Admin admin = adminMapper.selectById(adminId);
        toolBorrowRecords.setApprover(admin.getUsername());
        Long toolId = toolBorrowRecords.getToolId();
            Tools tool = toolsMapper.selectById(toolId);
                toolBorrowRecords.setToolStatus(2);//0:已消耗，1:使用中，2:已归还
                toolBorrowRecords.setStatus("已归还");
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        // 将LocalDateTime转换为Date
        Date date = Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant());
// 将java.util.Date转换为java.sql.Date
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());
        toolBorrowRecords.setReturnTime(sqlDate);//设置归还时间
                tool.setTotal(tool.getTotal() + toolBorrowRecords.getBnum());
                tool.setLent(tool.getLent() - toolBorrowRecords.getBnum());
                toolBorrowRecordsMapper.updateById(toolBorrowRecords);
                toolsMapper.updateById(tool);//更新工具库存
                return "工具归还成功";
    }
    /**
     * 驳回归还申请
     * @param id 员工id
     * @param adminId 管理员id
     */
    @Override
    public void refuseReturn(Integer id,Integer adminId) {
        ToolBorrowRecords toolBorrowRecords = toolBorrowRecordsMapper.selectById(id);
        toolBorrowRecords.setToolStatus(4);//0:已消耗，1:使用中，2:已归还
        toolBorrowRecords.setStatus("驳回归还");
        Admin admin = adminMapper.selectById(adminId);
        toolBorrowRecords.setApprover(admin.getUsername());
        toolBorrowRecordsMapper.updateById(toolBorrowRecords);
    }

    /**
     * 同意借用申请
     * @param id 员工id
     * @param adminId 管理员id
     */
    @Override
    public String agreed(Integer id,Integer adminId) {
        ToolBorrowRecords toolBorrowRecords = toolBorrowRecordsMapper.selectById(id);

        Admin admin = adminMapper.selectById(adminId);
        toolBorrowRecords.setApprover(admin.getUsername());
        toolBorrowRecordsMapper.updateById(toolBorrowRecords);

        String bcType = toolBorrowRecords.getBcType();
        Date borrowTime = toolBorrowRecords.getBorrowTime();
        Date returnTime = toolBorrowRecords.getReturnTime();
        Long employeeId = toolBorrowRecords.getEmployeeId();
        Long toolId = toolBorrowRecords.getToolId();

//        System.out.println("bcType是："+bcType);
        if (bcType.equals("consumed")){//消耗，对应的tool数扣相应的数量
            Tools tool = toolsMapper.selectById(toolId);
//            System.out.println(tool.getTotal()+"对比"+toolBorrowRecords.getBnum());
            if(tool.getTotal() >= toolBorrowRecords.getBnum()){
                toolBorrowRecords.setStatus("同意");
                toolBorrowRecords.setToolStatus(0);//已消耗//出库
                tool.setTotal(tool.getTotal() - toolBorrowRecords.getBnum());
                tool.setConsumed(tool.getConsumed() + toolBorrowRecords.getBnum());
                toolBorrowRecordsMapper.updateById(toolBorrowRecords);
                toolsMapper.updateById(tool);
                return "消耗成功";
            }else {
                return "库存不足";
            }
        } else {
//            System.out.println("消耗？？");
            Tools tool = toolsMapper.selectById(toolId);
//            System.out.println(tool.getTotal()+"对比"+toolBorrowRecords.getBnum());
            if(tool.getTotal() >= toolBorrowRecords.getBnum()){
                toolBorrowRecords.setStatus("同意");
                toolBorrowRecords.setToolStatus(1);//使用中
                tool.setLent(tool.getLent() + toolBorrowRecords.getBnum());
                tool.setTotal(tool.getTotal() - toolBorrowRecords.getBnum());
                toolBorrowRecordsMapper.updateById(toolBorrowRecords);
                toolsMapper.updateById(tool);
                return "借用成功";
            }else {
                return "库存不足";
            }

        }
    }
    //用户归还时，也要通过管理员，。。。。。。。
    //逻辑：：


    /**
     * 驳回借用申请
     * @param id 员工id
     * @param adminId 管理员id
     */
    @Override
    public void refuse(Integer id,Integer adminId) {
        ToolBorrowRecords toolBorrowRecords = toolBorrowRecordsMapper.selectById(id);
        toolBorrowRecords.setStatus("驳回");
        Admin admin = adminMapper.selectById(adminId);
        toolBorrowRecords.setApprover(admin.getUsername());
        toolBorrowRecordsMapper.updateById(toolBorrowRecords);
    }

    /**
     * 员工撤销工具出库申请
     * @param id 员工申请id
     */
    @Override
    public void cancelToolApplication(Integer id) {
        toolBorrowRecordsMapper.deleteById(id);
    }



}
